/*package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.aspect.RecordLogAnno;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service// 把此类的对象交给了 ioc 管理
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept>implements DeptService {
    // 获取持久层对象
    @Autowired
    private DeptMapper deptMapper;


    // ... existing code ...
    /**
     * 查询所有部门
     *
     * @return 部门集合
     */
   /* @Override
    public List<Dept> queryDepartments() {
        List<Dept> depts = deptMapper.selectList(null);
        // 返回给控制层
        return depts;
    }
// ... existing code ...


    /**
     * 根据 id 删除部门
     *
     * @param id
     */
   /* @Override
    @RecordLogAnno
    public void deleteDeptById(Integer id) {
        deptMapper.deleteById(id);
    }

    /**
     * 新增部门
     *
     * @param dept 部门对象
     */
   /* @Override
    @RecordLogAnno
    public void insertDept(Dept dept) {
        // 补全属性 , 因为dept中只有部门名, 没有创建时间和最后修改时间
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }

    /**
     * 根据 id 查询部门
     *
     * @param id 编号
     * @return 部门对象
     */
   /* @Override
    public Dept getDeptById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    /**
     * 修改部门
     *
     * @param dept 部门对象
     */
   /* @Override
    @RecordLogAnno
    public void updateDept(Dept dept) {
        // 补全属性, 最后修改时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }
}*/
package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> findAll() {
        //return deptMapper.selectList(null);
        return list();
    }

    @Override
    public void deleteById(Integer id) {
        //1. 判断部门下是否有员工， 如果有， 需要提示错误信息
        Integer count = empMapper.countByDeptId(id);
        if(count > 0){
            throw new BusinessException("部门下有员工， 不能删除");
        }

        //2. 删除部门
        //deptMapper.deleteById(id);
        removeById(id);
    }

    @Override
    public void add(Dept dept) {
        //1. 补全基础属性 - createTime, updateTime
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //2. 调用Mapper接口方法插入数据
        //deptMapper.insert(dept);
        save(dept);
    }

    @Override
    public Dept getDeptById(Integer id) { //这个方法需要改名，不然会跟MP提供的方法冲突，接口也要改
        //return deptMapper.selectById(id);
        Dept dept = getById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        //1. 补全基础属性-updateTime
        dept.setUpdateTime(LocalDateTime.now());

        //2. 调用Mapper接口方法更新部门
        //deptMapper.update(dept);
        updateById(dept);

    }
}
